<?php
namespace App\Services\PermissionsService;
/**
 * 权限管理
 *
 * 权限管理Service
 *
 * @author    zhouchao <zhouchao@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */
use App\Permission;
class PermissionsService
{
    public function getPermissionParent($parent = 0)
    {
        return $this->menu('is_menu',1);
    }

    public function menu($field, $value)
    {
        $menus = Permission::where($field,$value)
            ->select('id', 'display_name','parent_id','sort','name','icon')
            ->orderBy('sort','desc')
            ->get()
            ->toArray();
        if($menus){
            $menuList = $this->sortMenu($menus);
            foreach ($menuList as $key => &$v) {
                if ($v['child']) {
                    $sort = array_column($v['child'], 'sort');
                    array_multisort($sort,SORT_DESC,$v['child']);
                }
            }
            return $menuList;
        }
        return [];
    }

    public function sortMenu($menus,$pid=0)
    {
        $arr = [];
        if (empty($menus)) {
            return '';
        }
        foreach ($menus as $key => $v) {
            if ($v['parent_id'] == $pid) {
                $arr[$key] = $v;
                $arr[$key]['child'] = self::sortMenu($menus,$v['id']);
            }
        }
        return $arr;
    }

    public function sortMenuObject($menus,$pid=0)
    {
        foreach($menus as $menu){
            $child = self::childMenu($menu);
            $menu->child = $child;
            self::sortMenuObject($child);
        }
        return $menus;
    }

    public function childMenu($menu)
    {
        $menuslist = $this->findByField('parent_id',$menu->id,'sort','desc',['id', 'display_name','description','parent_id','sort','name','icon']);
        return $menuslist;
    }

    public function findByField($field, $value, $sort = 'id', $sc = 'desc', $columns = ['*']){
        return Permission::where($field,$value)->select($columns)->orderBy($sort,$sc)->get();
    }

    public function getPermissionList($input,$pid=0)
    {
        $results = Permission::where('display_name','like','%'.$input['display_name'].'%')
            ->where('parent_id',0)
            ->orderBy('sort','desc')
            ->paginate($input['per_page']);
//        foreach($results as $result){
//            if ($result->parent_id == $pid) {
//                $result->child = self::childMenu($result);
//            }
//        }
        $results = $this->sortMenuObject($results);
        return $results;
    }

    public function getRolePermissionList(){
        //判断是否选择
        return $this->sortMenu($this->all(['id','display_name','parent_id'])->toArray());
    }

    public function all($columns = ['*']){
        return Permission::all($columns);
    }

    public function getMenuId($name)
    {
        $arr = [];
        $zdata = $this->findByField('name',$name)->toArray();
        if(empty($zdata)){
            return $arr;
        }
        $arr['zMenu'] = $zdata[0];
        if($arr['zMenu']['parent_id'] > 0){
            $fdata = $this->findByField('id',$arr['zMenu']['parent_id'])->toArray();
            $arr['fMenu'] = $fdata[0];
        }
        return $arr;
    }
}